//
//  SearchHuoYuanViewController.m
//  阿帕
//
//  Created by mac on 2020/1/7.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "SearchHuoYuanViewController.h"
#import "CYCityChooseView.h"
#import "GoodsTypeChooseViewController.h"

@interface SearchHuoYuanViewController ()<CYChooseRegionDelegate, GoodsTypeChooseDelegate>
{
    NSInteger _addressStatus;
    NSString *_shipperSubdivisionCode;//发货地址code
    NSString *_consigneeSubdivisionCode;//收货地址CODE
    NSString *_coalType;//货物类型编码（小类）
}

@end

@implementation SearchHuoYuanViewController

static const CGFloat itemHeight = 45;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _shipperSubdivisionCode = @"";
    _consigneeSubdivisionCode = @"";
    _coalType = @"";
    
    self.navigationItem.title = @"搜索货源";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self setupBaseView];
}

- (void)setupBaseView {
    
    NSArray *formsTitleArr = @[@"发货地",@"收货地",@"货物类型",@"内容"];
    NSArray *placeholderArr = @[@"请选择",@"请选择",@"请选择",@"货源单号/货物描述"];
    
    for (int i=0; i<formsTitleArr.count; i++) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 1 + itemHeight*i, ScreenW, itemHeight)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bgView];
        
        CGSize size = [DCSpeedy dc_calculateTextSizeWithText:formsTitleArr[i] WithTextFont:14 WithMaxW:1000];
        
        //输入框
        if (i<3) {
            //按钮
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(15 + size.width, 0, ScreenW-25-size.width, itemHeight);
            button.tag = 10 + i;
            [button setTitle:placeholderArr[i] forState:UIControlStateNormal];
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            button.titleLabel.font = font(14);
            [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
            [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:button];
            
            //            UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenW-20, 15, 10, 15)];
            //            arrow.image = image(@"gcy_icon_my_rArrow");
            //            [bgView addSubview:arrow];
            
        } else {
            
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenW-210, 0, 200, itemHeight)];
            textField.tag = 10 + i;
            textField.font = font(14);
            textField.text = @"";
            textField.textColor = Dark_TextColor;
            textField.textAlignment = NSTextAlignmentRight;
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderArr[i] attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
            [bgView addSubview:textField];
        }
        
        //标题
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 120, itemHeight)];
        textLabel.text = formsTitleArr[i];
        textLabel.textColor = Dark_TextColor;
        textLabel.font = font(14);
        textLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:textLabel];
        
        //分割线
        UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, itemHeight-1, ScreenW - 30, 1)];
        hLine.backgroundColor = BackgroundColor;
        [bgView addSubview:hLine];
    }
    
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn setBackgroundColor:Theme_ColorValue];
    cut(searchBtn, BTN_BORDER_RADIUS);
    searchBtn.tag = 100;
    [searchBtn addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(20, 4 * 45 + 20, ScreenW-40, 45);
    [self.view addSubview:searchBtn];
}

#pragma mark - GoodsTypeChooseDelegate
- (void)GetAllGoodsCodeString:(NSString *)codeString :(NSString *)nameString {
    UIButton *button = [self.view viewWithTag:12];
    [button setTitle:nameString forState:UIControlStateNormal];
    
    _coalType = codeString;
}

#pragma mark - CYChooseRegionDelegate
- (void)getAllInfoProvinceCode:(NSString *)provinceCode provinceName:(NSString *)provinceName cityCode:(NSString *)cityCode cityName:(NSString *)cityName countyCode:(NSString *)countyCode countyName:(NSString *)countyName {
    
    NSString *address = CJStringWithFormat(@"%@%@%@", provinceName, cityName, countyName);
    
    NSString *code = @"";
    if ([DCSpeedy isBlankString:countyCode] == NO) {
        code = countyCode;
    } else if ([DCSpeedy isBlankString:cityCode] == NO) {
        code = cityCode;
    } else if ([DCSpeedy isBlankString:provinceCode] == NO) {
        code = provinceCode;
    }
    
    if (_addressStatus == 0) {
        
        UIButton *button = [self.view viewWithTag:10];
        [button setTitle:address forState:UIControlStateNormal];
        _shipperSubdivisionCode = code;
        
    } else {
        
        UIButton *button = [self.view viewWithTag:11];
        [button setTitle:address forState:UIControlStateNormal];
        _consigneeSubdivisionCode = code;
        
    }
    
}

- (void)btnEventClick:(UIButton *)button {
    if (button.tag == 10) {
        //出发地
        _addressStatus = 0;
        
        [[CYCityChooseView shared] show];
        [CYCityChooseView shared].pageShowFlag = 1;
        [CYCityChooseView shared].delegate = self;
        
    } else if (button.tag == 11) {
        //收货地
        _addressStatus = 1;
        
        [[CYCityChooseView shared] show];
        [CYCityChooseView shared].pageShowFlag = 1;
        [CYCityChooseView shared].delegate = self;
        
    } else if (button.tag == 12) {
        //货物类型
        GoodsTypeChooseViewController *gtcvc = [[GoodsTypeChooseViewController alloc] init];
        gtcvc.delegate = self;
        [self.navigationController pushViewController:gtcvc animated:YES];
        
    } else {
        //搜索
        UITextField *contentTextField = [self.view viewWithTag:13];
        
        NSDictionary *dic = @{
            @"shipperSubdivisionCode": _shipperSubdivisionCode,//装货地点国家行政区划代码
            @"consigneeSubdivisionCode": _consigneeSubdivisionCode,//收货国家行政区划代码
            @"mainOrderNumber": contentTextField.text,//货源单号
            @"coalBigType": @"",//货物类型编码（大类）
            @"coalType": _coalType,//货物类型编码（小类）
        };
        if (self.delegate && [self.delegate respondsToSelector:@selector(GetSearchConditionResult:)]) {
            [self.delegate GetSearchConditionResult:dic];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}


@end

